package com.jcf.springboot.controller.business;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jcf.springboot.common.R;
import com.jcf.springboot.entity.business.Seat;
import com.jcf.springboot.service.SeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjj
 * @since 2021-04-15
 */
@CrossOrigin
@RestController
@RequestMapping("/seat")
public class SeatController {

    @Autowired
    private SeatService seatService;

    @GetMapping("/getList")
    public R getList(String id){
        QueryWrapper<Seat> wrapper = new QueryWrapper<>();
        wrapper.eq("play_id", id);
        List<Seat> list = seatService.list(wrapper);
        if(list != null){
            return R.ok().data(list).message("获取座位列表成功");
        }
        else {
            return R.error().message("获取座位列表失败");
        }
    }

    @PostMapping("/sellSeat")
    public R sellSeat(String seat,String playId){
            String[] seats = seat.split(",");
        for (String s : seats) {
           int x =  Integer.parseInt(s.substring(0,s.indexOf("排")));
           int y = Integer.parseInt(s.substring(s.indexOf("排")+1,s.indexOf("座")));
            UpdateWrapper<Seat> wrapper = new UpdateWrapper<>();
            wrapper.eq("xaxis", x);
            wrapper.eq("yaxis", y);
            wrapper.eq("play_id", playId);
            Seat seat1 = new Seat();
            seat1.setStatus("1");
            seat1.setXaxis(x);
            seat1.setYaxis(y);
            boolean b = seatService.update(seat1, wrapper);
           if(!b){
                return R.error().message("座位售出失败");
            }
        }
        return R.ok().message("座位售出成功");
    }

}

